<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script> -->
     <!-- 引入Vue 全局注册一个名为Vue的构造函数 -->
 <script src="./js/vue.js"></script>
    <title>列表</title>
</head>
<!-- 案例：列表过滤 实现搜索框功能的实现 -->
<body>
    <div id="root">
   请输入:<input placeholder="请输入关键字" v-model="name">
   <ul>
    <li v-for="p in filterPerson" :key="p.id">{{p.name}}--{{p.sex}}--{{p.age}},<input type="text"></li>
   </ul>
    <button @click="sortType=1">升序</button>
    <button @click="sortType=2">降序</button>
    <button @click="sortType=0">原顺序不变</button>
    </div>
    <script>
      var vm= new Vue({
      el: '#root',
      data: {
        sortType:0,
        name:'',
        person:[
            {"id":001,"name":"李彤康",'age':22,'sex':'男'},
            {"id":002,"name":"李如萍",'age':19,'sex':'女'},
            {"id":003,"name":"冯雅琪",'age':23,'sex':'女'},
            {"id":004,"name":"程丽娜",'age':25,'sex':'女'}
        ],
        // filterPerson:[]
      },
      computed:{
        filterPerson(){
            const arr= this.person.filter((p)=>{
                return p.name.indexOf(this.name)!==-1;
            })
          if(this.sortType){
            arr.sort((p1,p2)=>{
                return this.sortType==1? p1.age-p2.age:p2.age-p1.age;
            })
          }
          return arr;
        }
      }
    //  watch:{
    //   name:{
    //     immediate:true,
    //     handler(val){
    //         this.filterPerson=this.person.filter((p)=>{
    //             return p.name.indexOf(val)!==-1;
    //         })
    //     }
    //   }
    //  }
      }
      )
    </script>
</body>
</html>